import 'dart:io';

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_router/shelf_router.dart';

const _jsonHeaders = {
  'content-type': 'application/json',
};

void main() async {
  var app = Router();

  var handler = const Pipeline().addMiddleware(logRequests()).addHandler(app);

  app.post('/api_app/login', _loginController);

  await io.serve(handler, '0.0.0.0', 8090);
}

Future _loginController(Request request) async {
  return _response('login.json');
}


Future<Response> _response(String fileName) async {
  String response = await readFile('response_json/$fileName');
  await Future.delayed(Duration(seconds: 1));
  return Response.ok(response, headers: _jsonHeaders);
}

Future<String> readFile(String fileName) async {
  var config = File(fileName);
  return config.readAsString();
}
